/**
 * 拜访记录维护
 * @author miaoyl
 * @date 2019/04/17
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {
            GWBHACTI:'N'
        },
        gwbflist={},
        cols = [[
            {type:'checkbox',fixed: 'left'},
            {field: 'GWBH003', width: 100, title: '拜访日期', fixed: 'left',sort: true,
                search:'default',searchOpts: {type: 'daterange'}
            }
            , {field: 'GWBGL003', width: 160, title: '商机主题'}
            , {field: 'GWBHID', width: 160, title: '客户编号',search:'default', searchOpts: {icon: true}}
            , {field: 'GWAKL004', width: 160, title: '客户名称'}
            , {field: 'GWBH016', width: 160, title: '经销商编号'}
            , {field: 'GWAKL003', width: 160, title: '经销商名称'}
            , {field: 'GWAVL003', width: 160, title: '商机阶段',
                search:'default', searchOpts: {icon: true}
            }
            , {field: 'GWBH006', width: 160, title: '拜访人员'}
            , {field: 'GWBH031', width: 160, title: '成交几率'}
            , {field: 'MONEY', width: 160, title: '商机金额'}
            , {field: 'GWBH010', width: 160, title: '拜访目的'}
            , {field: 'GWBH011', width: 160, title: '拜访内容',
                templet:function (data) {
                    var d = data.GWBH011;
                    var result = "";
                    if (d && d.indexOf('{') != -1) {
                        d = JSON.parse(d);
                        $.each(gwbflist, function (i, val) {
                            var item = d[val.GWBF002];
                            if (item) {
                                result += ('<p>【' + val.GWBFL004 + '】' + item + "</p>");
                            }
                        });
                    }
                    return result;
                }
            }
            , {field: 'GWBH032', width: 160, title: '主管回复'}
            , {field: 'GWBH040', width: 160, title: '领导点评',
                templet:function (data) {
                    var d = data.GWBH040;
                    var result = "";
                    if (d && d.indexOf('{') != -1) {
                        d = JSON.parse(d);
                        $.each(d, function (i, val) {
                            result += ('<p>【' + val.name + '】' + val.msg + "</p>");
                        });
                    }
                    return result;
                }
            }
            , {field: 'GWBH034', width: 160, title: '维系方式'}
            , {field: 'GWBH001', width: 160, title: '商机来源'}
            , {field: 'GWBH002_NAME', width: 160, title: '业务人员',
                search:'default', searchOpts: {icon: true}
            }
            , {field: 'NAME_USER', width: 160, title: '业务部门',
                search:'default', searchOpts: {icon: true}
            }
            , {field: 'GWBHACTI', width: 160, title: '状态',
                filterDatas: app.datas_('gwat160_gwbhacti').concat([{code:'', value:'全部'}]),
                search:'default', searchOpts: {type: 'select', default: 'N'}
            },
            {
                width: 60,
                title: '',
                toolbar: '#script-gwat160setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwat160SettingAction,
        moduleName = '拜访记录'
    ;

    app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwat160',
                _searchs: [{
                    id: 'gwat160', tableId: 'gwat160_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwat160_edit', tpl: 'gwat160_edit'
                },{
                    dom: 'gwat160_detail', tpl: 'gwat160_detail'
                }, {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }/*, {
                    dom: 'common_open_gwak', tpl: 'common/_open_gwak'
                },{
                    dom: 'common_open_gwav', tpl: 'common/_open_gwav'
                },{
                    dom: 'common_open_ywy', tpl: 'common/_open_ywy'
                },{
                    dom: 'common_open_org', tpl: 'common/_open_org'
                }*/]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwat160_table'),
                    url: g_ctxPath + 'crm/gwat160/getGwbhlist',
                    exportsUrl: g_ctxPath + 'crm/gwat160/getGwbhlist',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    id: 'gwat160_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_where
                });

                layui.laytpl(app.getByid(doc, 'script-gwat160setting-action').innerHTML).render({}, function (string) {
                    gwat160SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwat160_table)', function (obj) {

                    layer.tips(gwat160SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwat160_edit',
                                            tagData: {
                                                moduleName: moduleName,
                                                obj:obj
                                            },
                                            index: 0,
                                            openOpts:{
                                                title: moduleName +'信息编辑',
                                                minmax: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwat160_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwat160_detail',
                        tagData: {
                            obj:obj.data
                        },
                        index: 1,
                        openOpts: {
                            title: moduleName + "详情",
                            minmax: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwat160_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwat160_edit',
                                tagData: {
                                    is_add: true,
                                    moduleName:moduleName
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '信息编辑',
                                    minmax: true
                                }
                            });
                            break;
                        case 'del':
                            self.delDataBatch();
                            break;
                        case 'detail':
                            self.openDetail();
                            break;
                        case 'check':
                            self.check();
                            break;
                        case 'unCheck':
                            self.unCheck();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwat160_table', where: table_where});
        },

        delData: function (obj) {
            app.ajax({
                xurl: 'crm/gwat160/del',
                xdata: obj.data,
                loading: true,
                confirm: {msg: "是否删除选择的数据？"},
                okmsg: moduleName + '删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });

        },

        delDataBatch: function(){
            var checkStatus = table.checkStatus('gwat160_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if(v.GWBHACTI != 'N'){
                        app.showWarning("只有单据状态为N：未审核才可以删除!");
                        flag = false;
                        return ;
                    }
                    var _temp = app.fields_('GWBH_T');
                    _temp.GWBHNAME = v.GWBHNAME;
                    _temp.GWBHID = v.GWBHID;
                    _temp.GWBHSEQ = v.GWBHSEQ;
                    _xdata.push(_temp);
                });
                if(flag) {
                    app.ajax({
                        xurl: 'crm/gwat160/delDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否删除选择的数据？"},
                        okmsg: moduleName + '信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        check: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwat160_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWBH_T');
                    if (v.GWBHACTI != 'N') {
                        app.showWarning("只有状态为N：未审核的单据才可以审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWBHNAME = v.GWBHNAME;
                        _temp.GWBHID = v.GWBHID;
                        _temp.GWBHSEQ = v.GWBHSEQ;
                        _temp.GWBHACTI = 'Y';
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.ajax({
                    xurl: 'crm/gwat160/updateData',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否审核选择的数据？"},
                    okmsg: '单据审核成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        unCheck: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwat160_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWBH_T');
                    if (v.GWBHACTI != 'Y') {
                        app.showWarning("只有状态为Y：审核的单据才可以取消审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWBHNAME = v.GWBHNAME;
                        _temp.GWBHID = v.GWBHID;
                        _temp.GWBHSEQ = v.GWBHSEQ;
                        _temp.GWBHACTI = 'N';
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.ajax({
                    xurl: 'crm/gwat160/updateData',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否取消审核选择的数据？"},
                    okmsg: '单据取消审核成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        openDetail: function(){
            var checkStatus = table.checkStatus('gwat160_table');
            if(checkStatus.data.length > 1){
                app.showWarning("该业务只能进行单笔操作！");
                return;
            }
            if (checkStatus.data.length > 0) {
                self._openTplFull({
                    tag: 'gwat160_detail',
                    tagData: {
                        obj:checkStatus.data[0]
                    },
                    index: 1,
                    openOpts: {
                        title: moduleName + "详情",
                        minmax: true
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwat160',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWBHID') {//客户
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWBHID = data.GWAK001;
                                        }
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field == 'GWAVL003') {//商机阶段
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwav001',
                                            columnList: [{label: '商机阶段编号', prop: 'GWAV001', width: '150'},
                                                {label: '商机阶段说明', prop: 'GWAVL003', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWAVL003 = data.GWAV001;
                                        }
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择商机阶段',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field == 'GWBH002_NAME') {//业务人员
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysuser_id',
                                            columnList: [{label: '业务员编号', prop: 'ID', width: '150'},
                                                {label: '业务员姓名', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWBH002_NAME = data.ID;
                                        }
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择业务人员',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field == 'NAME_USER') {//业务部门
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysorg_id',
                                            columnList: [{label: '部门编号', prop: 'ID', width: '150'},
                                                {label: '部门名称', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.NAME_USER = data.ID;
                                        }
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择业务部门',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        },
                    },
                    watch: {
                        'isearch.fields.GWBH003': function (newValue, oldValue) {
                            if (newValue.length == 2) {
                                this.isearch.fields.GWBH003_START = newValue[0];
                                this.isearch.fields.GWBH003_END = newValue[1];
                            } else {
                                this.isearch.fields.GWBH003_START = '';
                                this.isearch.fields.GWBH003_END = '';
                            }
                        }
                    }
                }]
            });
            app.ajax({
                xurl: 'crm/gwat160/listGwbf',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        gwbflist = result.data.data;
                        self.initTable();
                    }
                }
            });
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});